/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kamus.manager;

import com.kamus.DB;
import com.kamus.model.PasanganKata;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PasanganKataGIMgr extends BaseMgr{

    private static PasanganKataGIMgr instance;

    public PasanganKataGIMgr() {
        connection = getConnection();
    }

    public static PasanganKataGIMgr getInstance() {
        if (instance == null) {
            instance = new PasanganKataGIMgr();
        }
        return instance;
    }

    public void insertPasKata(PasanganKata pasKata) {
        String[] arrKataGaul = pasKata.getGaul().split(";");
        int[] idGaul = new int[arrKataGaul.length];
        String[] arrKataInd = pasKata.getIndonesia().split(";");
        int[] idInd = new int[arrKataInd.length];
        for (int i = 0; i < arrKataGaul.length; i++) {
            for (int j = 0; j < arrKataInd.length; j++) {
                idGaul[i]=KosakataGaulMgr.getInstance().cekOrInsert(arrKataGaul[i]);
                idInd[j] = KosakataIndMgr.getInstance().cekOrInsert(arrKataInd[j]);
                insert(idGaul[i], idInd[j]);
            }
        }
    }

    public void insert(int idGaul, int idInd) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("insert into GAUL_IND(ID_GAUL,ID_IND) values(?,?)");
            psmnt.setInt(1, idGaul);
            psmnt.setInt(2, idInd);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteKata(int id) {
        cekConnection();
        try {
            psmnt = connection.prepareStatement("delete from GAUL_IND where ID=?");
            psmnt.setInt(1, id);
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<PasanganKata> getKatas() {
        cekConnection();
        ArrayList<PasanganKata> list = new ArrayList<PasanganKata>();
        PasanganKata kata;
        try {
            psmnt = connection.prepareStatement("select gi.id as id, kg.kosakata as gaul, ki.kosakata as ind from gaul_ind gi,kosakata_gaul kg ,kosakata_ind ki where gi.id_gaul=kg.id and gi.id_ind=ki.id order by gaul");
            rs = psmnt.executeQuery();
            while (rs.next()) {
                kata = new PasanganKata();
                kata.setId(rs.getInt("id"));
                kata.setGaul(rs.getString("gaul"));
                kata.setIndonesia(rs.getString("ind"));
                list.add(kata);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }

    public PasanganKata getKata(String id) {
        cekConnection();
        PasanganKata pKata=null;
        try {
            psmnt = connection.prepareStatement("select gi.id as id, kg.kosakata as gaul, ki.kosakata as ind from gaul_ind gi,kosakata_gaul kg ,kosakata_ind ki where gi.id_gaul=kg.id and gi.id_ind=ki.id and gi.id=?");
            psmnt.setString(1, id);
            rs = psmnt.executeQuery();
            while (rs.next()) {
                pKata = new PasanganKata();
                pKata.setId(rs.getInt("id"));
                pKata.setIndonesia(rs.getString("ind"));
                pKata.setGaul(rs.getString("gaul"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pKata;
    }

    public void updateKata(PasanganKata kata) {
        cekConnection();
        int idGaul = KosakataGaulMgr.getInstance().cekOrInsert(kata.getGaul());
        int idInd = KosakataIndMgr.getInstance().cekOrInsert(kata.getIndonesia());
        try {
            psmnt = connection.prepareStatement("update GAUL_IND set ID_GAUL=?, ID_IND=? where ID= ?");
            psmnt.setInt(1, idGaul);
            psmnt.setInt(2, idInd);
            psmnt.setInt(3, kata.getId());
            psmnt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
